# -*- coding = utf-8 -*-
# @Time : 2024/3/15 9:09
# @File : area_code_get.py
# @Software : PyCharm
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
import time
from selenium import webdriver
import undetected_chromedriver as uc
import pymysql

#已完成爬虫 可见数据库

options = webdriver.ChromeOptions()
options.add_argument(r"--user-data-dir=C:\Users\12036\AppData\Local\Google\Chrome\User Data")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
browser = webdriver.Chrome(options=options)
wait = WebDriverWait(browser, 10)  # 超时时长为10s

url ="https://www.mca.gov.cn/mzsj/xzqh/2022/202201xzqh.html"
browser.get(url)
div_sons = browser.find_elements(By.XPATH, "/html/body/div[1]/table/tbody/tr")

data_list = []
for i in range(4,len(div_sons)-7):#len(div_sons)-7
    data = []
    #行政编码
    data.append(browser.find_element(By.XPATH, '//*[@id="2022年中华人民共和国县以上行政区划代码_32365"]/table/tbody/tr['+str(i)+']/td[2]').text)
    #省市名称
    data.append((browser.find_element(By.XPATH,'.//*[@id="2022年中华人民共和国县以上行政区划代码_32365"]/table/tbody/tr[' + str(i) + ']/td[3]').text).strip())
    data_list.append(data)
# print(data_list)

dbpath = "poetry_information.db"
db = pymysql.connect(host='127.0.0.1', port=3306, user='root', db='poetry_information', password='root', charset='utf8')
cursor = db.cursor()
cursor.execute("DROP TABLE IF EXISTS area_code")
sql='''
    create table area_code
    (
    code TEXT ,
    area_name TEXT
    ) 
    '''
cursor.execute(sql)

cur = db.cursor()
sql1 = '''
    INSERT INTO area_code(
    code,area_name)
    VALUES(%s,%s)
    '''
i=0
for i in range(0,len(data_list)-1):
    cur.execute(sql1,(data_list[i][0],data_list[i][1]))

db.commit()
cur.close()
db.close()

# 退出浏览器
browser.quit()

